package myStack;

import java.util.Stack;

class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minstack;
    public MinStack() {
        stack = new Stack<>();
        minstack = new Stack<>();
    }
    
    public void push(int val) {
        stack.push(val);
        if(minstack.empty()) {
            minstack.push(val);
        }else if(val <= minstack.peek()) {
            minstack.push(val);
        }
    }
    
    public void pop() {
        int n  = stack.pop();
        if(!minstack.empty()) {
            if(n == minstack.peek()) {
                minstack.pop();
            }
        }
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int getMin() {
        if(!minstack.empty()) {
            return minstack.peek();
        }
        return -1;
    }
}